package com.example.mychat;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * @Description
 * @Author susu
 * @Date 2024/9/3
 */
public class testSocketClient {
    public static void main(String[] args) {
        Socket socket = null;

        try {
            socket = new Socket("127.0.0.1",1024);
            OutputStream outputStream = socket.getOutputStream();
            PrintWriter printWriter = new PrintWriter(outputStream);


            new Thread(new Runnable() {
                @Override
                public void run() {

                    while (true) {
                        try {
                            System.out.println("请输入内容");
                            Scanner scanner = new Scanner(System.in);
                            String input = scanner.nextLine();
                            printWriter.println(input);
                            printWriter.flush();
                        }catch (Exception e){
                            e.printStackTrace();
                            break;
                        }
                    }
                }
            }).start();

            InputStream inputStream = socket.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true){
                        try {
                            String readData = bufferedReader.readLine();
                            System.out.println("收到服务端消息" + readData);

                        }catch (Exception e){
                            e.printStackTrace();
                            break;
                        }
                    }
                }
            }).start();

        } catch (IOException e) {
            throw new RuntimeException(e);

        }
    }
}
